<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 搜索条件
const userName = ref('')
const archiveType = ref('')
const dateRange = ref('')

// 表格数据
const tableData = ref([
  {
    id: 'A20240501001',
    userName: '张华明',
    archiveType: '健康记录',
    content: '血压监测数据',
    createTime: '2024/05/01 10:15',
    updateTime: '2024/05/01 10:15',
    status: '已保存'
  },
  {
    id: 'A20240502002',
    userName: '周明明',
    archiveType: '用药记录',
    content: '每日用药情况记录',
    createTime: '2024/05/02 09:30',
    updateTime: '2024/05/02 15:45',
    status: '已更新'
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 处理分页变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
}

// 处理每页显示条数变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
}

// 处理详情
const handleDetail = (row: any) => {
  router.push({
    path: `/user/archive-detail/${row.id}`,
    query: { ...row }
  })
}

// 处理编辑
const handleEdit = (row: any) => {
  console.log('编辑', row)
}

// 处理删除
const handleDelete = (row: any) => {
  console.log('删除', row)
}

// 处理导出
const handleExport = (row: any) => {
  console.log('导出', row)
}

// 处理分享
const handleShare = (row: any) => {
  console.log('分享', row)
}
</script>

<template>
  <div class="archive-list">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true">
        <el-form-item label="用户名">
          <el-input v-model="userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="档案类型">
          <el-select v-model="archiveType" placeholder="请选择档案类型">
            <el-option label="全部" value="" />
            <el-option label="健康记录" value="健康记录" />
            <el-option label="用药记录" value="用药记录" />
            <el-option label="饮食记录" value="饮食记录" />
            <el-option label="运动记录" value="运动记录" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary">搜索</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="id" label="档案ID" width="120" />
      <el-table-column prop="userName" label="用户名" width="100" />
      <el-table-column prop="archiveType" label="档案类型" width="100">
        <template #default="{ row }">
          <el-tag type="primary">{{ row.archiveType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容摘要" min-width="150" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="updateTime" label="更新时间" width="160" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === '已保存' ? 'success' : 'warning'">{{ row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" fixed="right">
        <template #default="{ row }">
          <div class="operation-buttons">
            <div class="button-row">
              <el-button size="small" @click="handleDetail(row)">详情</el-button>
              <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
            </div>
            <div class="button-row">
              <el-button size="small" type="success" @click="handleExport(row)">导出</el-button>
              <el-button size="small" type="info" @click="handleShare(row)">分享</el-button>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped lang="scss">
.archive-list {
  padding: 20px;

  .search-section {
    background: #f5f7fa;
    padding: 20px;
    border-radius: 12px;
    margin-bottom: 20px;
  }

  :deep(.el-table) {
    background: #f5f7fa;
    border-radius: 12px;
    overflow: hidden;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    background: #f5f7fa;
    padding: 20px;
    border-radius: 12px;
  }

  .operation-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .button-row {
    display: flex;
    gap: 5px;
    justify-content: center;
  }
}
</style>